﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;

namespace IHISCETeim.SvrCheck
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }
        private int GetSnycInterval()
        {
            int SnycInterval = 0;
            try
            {
                SnycInterval = Convert.ToInt32(ConfigurationManager.AppSettings["SnycInterval"]);
            }
            catch
            {
                SnycInterval = 5;
            }
            return SnycInterval * 1000 * 60;
        }
        protected override void OnStart(string[] args)
        {
           // serviceController1.st
            Common.WriteLog("IHISCE信息化管理系统检测服务已启动!");
            timer1.Interval = GetSnycInterval();//1分钟

            //timer1.Interval = 1000 * 10;
            timer1.Enabled = true;
            timer1.Start();
        }

        protected override void OnStop()
        {
            Common.WriteLog("IHISCE信息化管理系统检测服务已停止!");
        }

       

        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            ServiceController _controller;
            string[] svrName = new string[] { "IHISCETeim.AutoSendEMailSvr", "IHISCETeim.AutoSendSMSSvr", "IHISCETeim.SAPSyncSvr" };
            foreach (string itmSvr in svrName)
            {
                _controller = new ServiceController(itmSvr);
                if (_controller.Status.ToString() != "Running")
                {
                    string msg=string.Format("服务{0}不在运行中,当前状态为{1},请尽快启动.{2}[石川岛中骏]", _controller.ServiceName, _controller.Status.ToString(),System.DateTime.Now.ToString("yyyy-MM-dd"));
                    Common.WriteLog(msg);
                    IHISCETeim.SendSMSSvr.SMSSendController.SendSing("15980907855", msg, "");
                }
            }
        }
    }
}
